package com.ksd.common.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ExcelTool {
    // 查找符合命名规则的 Excel 文件
    public static LinkedHashSet<String> findMovieExcelFiles(String folderPath) {
        LinkedHashSet<String> filePaths = new LinkedHashSet<>();
        File folder = new File(folderPath);

        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    String fileName = file.getName();
                    // 判断文件是否以“电影”开头且以“.xlsx”结尾（若为.xls 需调整后缀判断）
                    if (fileName.startsWith("电影") && fileName.endsWith(".xlsx")) {
                        filePaths.add(file.getAbsolutePath());
                    }
                }
            }
        }else{
            String fileName = folder.getName();
            // 判断文件是否以“电影”开头且以“.xlsx”结尾（若为.xls 需调整后缀判断）
            if (fileName.startsWith("电影") && fileName.endsWith(".xlsx")) {
                filePaths.add(folder.getAbsolutePath());
            }
        }
        return filePaths;
    }

    public static LinkedHashSet<String> findMusicExcelFiles(String folderPath) {
        LinkedHashSet<String> filePaths = new LinkedHashSet<>();
        File folder = new File(folderPath);

        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    String fileName = file.getName();
                    // 判断文件是否以“电影”开头且以“.xlsx”结尾（若为.xls 需调整后缀判断）
                    if (fileName.startsWith("歌曲") && fileName.endsWith(".xlsx")) {
                        filePaths.add(file.getAbsolutePath());
                    }
                }
            }
        }else{
            String fileName = folder.getName();
            // 判断文件是否以“电影”开头且以“.xlsx”结尾（若为.xls 需调整后缀判断）
            if (fileName.startsWith("歌曲") && fileName.endsWith(".xlsx")) {
                filePaths.add(folder.getAbsolutePath());
            }
        }
        return filePaths;
    }

    // 读取 Excel 文件内容
    public static void readExcelFileByFileRootPath(String rootPath,File excelFile) {
        try (InputStream inputStream = new FileInputStream(excelFile);
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            for (Row row : sheet) {
                for (Cell cell : row) {
                    String cellValue = "";
                    // 根据单元格类型获取值
                    switch (cell.getCellType()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                cellValue = cell.getDateCellValue().toString();
                            } else {
                                cellValue = String.valueOf((long) cell.getNumericCellValue());
                            }
                            break;
                        case BOOLEAN:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        default:
                            cellValue = "";
                    }
                    System.out.print(cellValue + "\t");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static List<MovieExcel> readExcelMovieFile(File excelFile) {
        List<MovieExcel> movieExcels = new ArrayList<>();
        try (InputStream inputStream = new FileInputStream(excelFile);
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            for (Row row : sheet) {
                for (Cell cell : row) {
                    MovieExcel movieExcel = new MovieExcel();
                    String cellValue = "";
                    // 根据单元格类型获取值
                    switch (cell.getCellType()) {
                        case STRING:
                            cellValue = cell.getStringCellValue();
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                cellValue = cell.getDateCellValue().toString();
                            } else {
                                cellValue = String.valueOf((long) cell.getNumericCellValue());
                            }
                            break;
                        case BOOLEAN:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        default:
                            cellValue = "";
                    }
                    System.out.print(cellValue + "\t");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return movieExcels;
    }


    public static LinkedHashSet<String> findMovieExcelFileAll(String folderPath) {
        LinkedHashSet<String> filePaths = new LinkedHashSet<>();
        // 调用递归方法
        findMovieExcelFilesRecursive(new File(folderPath), filePaths);
        return filePaths;
    }

    private static void findMovieExcelFilesRecursive(File file, LinkedHashSet<String> filePaths) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File subFile : files) {
                    // 递归处理子文件/子文件夹
                    findMovieExcelFilesRecursive(subFile, filePaths);
                }
            }
        } else {
            String fileName = file.getName();
            // 判断文件是否以“电影”开头且以“.xlsx”结尾
            if (fileName.startsWith("电影") && fileName.endsWith(".xlsx")) {
                filePaths.add(file.getAbsolutePath());
            }
        }
    }

    public static LinkedHashSet<String> findMusicExcelFileAll(String folderPath) {
        LinkedHashSet<String> filePaths = new LinkedHashSet<>();
        // 调用递归方法
        findMusicExcelFilesRecursive(new File(folderPath), filePaths);
        return filePaths;
    }

    private static void findMusicExcelFilesRecursive(File file, LinkedHashSet<String> filePaths) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File subFile : files) {
                    findMusicExcelFilesRecursive(subFile, filePaths);
                }
            }
        } else {
            String fileName = file.getName();
            if (fileName.startsWith("歌曲") && fileName.endsWith(".xlsx")) {
                filePaths.add(file.getAbsolutePath());
            }
        }
    }
}
